<html>
<head><meta charset="utf-8"><title>array patterns · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html">array patterns</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="154693178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693178">(Jan 09 2019 at 01:52)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span> and I were chatting about sublice patterns and future-proofing for array patterns</p>



<a name="154693192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693192">(Jan 09 2019 at 01:52)</a>:</h4>
<p>dear lord there are so many streams.. Zulip is <span class="emoji emoji-1f641" title="frown">:frown:</span></p>



<a name="154693210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693210">(Jan 09 2019 at 01:53)</a>:</h4>
<p>(I'm also not sure this is the right one- "const eval" is more "evaluating consts" than "typechecking constants"  Ithink, but seems a reasonable place)</p>



<a name="154693233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693233">(Jan 09 2019 at 01:54)</a>:</h4>
<p>anyways the question was how hard it would be to support e.g. <code>let [x, y, z @ ..] = [1, 2, 3, 4];</code> which would result in <code>z: [i32; 2]</code></p>



<a name="154693282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693282">(Jan 09 2019 at 01:55)</a>:</h4>
<p>My intuition was that it would be quite difficult, as you'd have to do subtraction from the const in <code>[i32; 4]</code> based on the # of other elements bound in the pattern</p>



<a name="154693290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693290">(Jan 09 2019 at 01:55)</a>:</h4>
<p>It's not a simple syntactic element count like array length inference is today</p>



<a name="154693364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693364">(Jan 09 2019 at 01:57)</a>:</h4>
<p>I'm not sure how the compiler is architechted around this.. but ostensibly we already know the type of <code>RHS = [1, 2, 3, 4]</code> so we know the type of the pattern; once we know the type of the pattern <code>xs = [x, y, z, @ ..]</code> we can infer the type of <code>z</code> using the known type of the pattern and the number of elements not in <code>z</code></p>



<a name="154693430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693430">(Jan 09 2019 at 01:59)</a>:</h4>
<p>and <code>let [a, b, c, d] = [1, 2, 3, 4]</code> already works but <code>let [a, b, d, e] = [1,2,3,4]</code> does not so unification of the pattern type and the expression already happens here</p>



<a name="154693499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693499">(Jan 09 2019 at 02:00)</a>:</h4>
<p>typing <code>[a, b, c @ ..]</code> cannot be done without knowing <code>[1, 2, 3, 4]</code> tho</p>



<a name="154693592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693592">(Jan 09 2019 at 02:03)</a>:</h4>
<p>I'd think doing this properly would require const unification</p>



<a name="154693952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693952">(Jan 09 2019 at 02:12)</a>:</h4>
<p><span class="user-mention" data-user-id="127859">@Taylor Cramer</span> something like this:</p>
<div class="codehilite"><pre><span></span>Γ ⊢tm   N : usize
Γ ⊢tm   M : usize
Γ ⊢size N &lt; M
Γ ⊢ty   σ type
Γ ⊢tm   arr: [σ; N]
---------------------------------------
Γ ⊢pat  [elt_0, .., elt_M, tail] : [σ; N]
Γ ⊢pat  tail : [σ; N - M]
∀ i ∈ M. Γ ⊢ elt_i : σ
</pre></div>



<a name="154693969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154693969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154693969">(Jan 09 2019 at 02:13)</a>:</h4>
<p>probably requires const unification yeah</p>



<a name="154706408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154706408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154706408">(Jan 09 2019 at 07:49)</a>:</h4>
<blockquote>
<p>anyways the question was how hard it would be to support e.g. <code>let [x, y, z @ ..] = [1, 2, 3, 4];</code> which would result in <code>z: [i32; 2]</code></p>
</blockquote>
<p>We already support that using <code>z..</code>, so not very.</p>



<a name="154735627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154735627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154735627">(Jan 09 2019 at 16:14)</a>:</h4>
<p>oh well don't i feel foolish</p>



<a name="154735655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154735655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154735655">(Jan 09 2019 at 16:15)</a>:</h4>
<p>Here was me thinking that <code>#![feature(slice_patterns)]</code> was just for slices</p>



<a name="154735681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154735681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154735681">(Jan 09 2019 at 16:15)</a>:</h4>
<p>that certainly makes things simpler</p>



<a name="154735762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154735762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154735762">(Jan 09 2019 at 16:16)</a>:</h4>
<p><span class="user-mention" data-user-id="127859">@Taylor Cramer</span>  I was trying to say it wasn't before on Discord :P -- I think we miscommunicated a lot ^.^</p>



<a name="154735800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154735800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154735800">(Jan 09 2019 at 16:16)</a>:</h4>
<p>In my defense, there were no examples of this in the RFC</p>



<a name="154735817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/array%20patterns/near/154735817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/array.20patterns.html#154735817">(Jan 09 2019 at 16:17)</a>:</h4>
<p>very true</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>